//
//  ViewController.swift
//  1.15.Adding Buttons to Navigation Bars Using UIBarButtonItem
//
//  Created by joker on 15/7/22.
//  Copyright (c) 2015年 joker. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
     let items = [ "Up", "Down"]
    func performAdd(sender: UIBarButtonItem) {
        println("Add method got called")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        /* #1 */
//        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: "performAdd:")
        
        /* #2 */
//        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "performAdd:")
        
        /* #3 */
//        var simpleSwitch = UISwitch()
//        simpleSwitch.setOn(true, animated: true)
//        simpleSwitch.tintColor = UIColor.redColor()
//        simpleSwitch.thumbTintColor = UIColor.blackColor()
//        simpleSwitch.onTintColor = UIColor.greenColor()
//        
//        simpleSwitch.addTarget(self, action: "switchHandler:", forControlEvents: .ValueChanged)
//        
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: simpleSwitch)
        
        
        /* #4 */
        let segmentedController = UISegmentedControl(items: items)
        segmentedController.momentary = true
        
        segmentedController.addTarget(self, action: "segmentedControllerTapped:", forControlEvents: .ValueChanged)
        
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: segmentedController)

        let rightButtonItem = UIBarButtonItem(customView: segmentedController)
        navigationItem.setRightBarButtonItem(rightButtonItem, animated: true)
    }
    func segmentedControllerTapped(sender: UISegmentedControl)
    {
        if sender.selectedSegmentIndex < items.count {
            
            println(items[sender.selectedSegmentIndex])
        }
        else
        {
            println("Unknown button is pressed")
        }
    }

    func switchHandler(sender: UISwitch) {
        if sender.on {
            println("switch.state is on")
        }
        else
        {
            println("switch.state is off")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

